<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<form action="/cron/qty.php">
<input type="text" id="distcode" name="distcode" value="<?php echo $_GET['distcode']; ?>"><input type='submit' name='submit' value="Check" />
</form>

<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

$no=0;
$ucode = $_GET['distcode'];
if(($db_msg=cron_initialize()) !== true){
	print_r("Connection error");
}else{
	if($ucode<>"")
		$record_sql="SELECT d.code,d.name,(sum(gg_sales_bv.bv)/100) as quantity FROM `gg_sales_bv` inner join gg_users d on d.id=gg_sales_bv.uid and d.status='normal' inner join gg_users s on d.ref_left between s.ref_left and s.ref_right and s.code='".$ucode."' where gg_sales_bv.bv>0 group by d.name order by d.ref_left";
	else
		$record_sql="SELECT d.code,d.name,(sum(gg_sales_bv.bv)/100) as quantity FROM `gg_sales_bv` inner join gg_users d on d.id=gg_sales_bv.uid and d.status='normal' inner join gg_users s on d.ref_left between s.ref_left and s.ref_right and s.id=38 where gg_sales_bv.bv>0 group by d.name order by d.ref_left";
		
	$t = @mysql_num_rows($r=mysql_query($record_sql));
	
	if($t<=0)
	{
		print_r("no record");
		exit;
	}
	
	echo "<table border='1'><tr><td>No.</td><td>Code</td><td>Name</td><td>Quantity</td></tr>";
	$i=0;
	$total=0;
	while($user = mysql_fetch_assoc($r))
	{		
		$i++;
		echo "<tr><td>".$i."</td><td>".$user['code']."</td><td>".$user['name']."</td><td align='right'>".number_format($user['quantity'],0)."</td></tr>";
		$total+=$user['quantity'];
	}
	echo "<tr><td colspan=3 align='right'>Total</td><td align='right'>".number_format($total,0)."</td></tr>";
	echo "</table>";


}

exit;		
?>